#include "Thread.h"
#include <process.h>
#include <iostream>

Thread::Thread()
	: mThread(NULL)
	, mState( State_None )
{
}

Thread::~Thread()
{
}

bool Thread::Start(ThreadHandle handle, void *param, unsigned int stack_size, PriorityType priority)
{
	int flag = 0;
	if (Priority_Suspend == priority)
		flag = CREATE_SUSPENDED;
	
	unsigned int id = 0;
	mThread = (HANDLE)_beginthreadex( NULL, stack_size, handle, param, flag, &id );

	if (mThread != NULL)
		mState = (Priority_Suspend == priority ? State_Suspend : State_Run);

	return 0 != mThread;
}

void Thread::Final()
{
	if (mThread == NULL)
		return;

	mState = State_Final;
	Join();
}

void Thread::Suspend()
{
	::SuspendThread(mThread);
}

void Thread::Resume()
{
	::ResumeThread(mThread);
}

void Thread::Join()
{
	if (mThread == NULL)
		return;

	WaitForSingleObject( mThread, INFINITE );
	CloseHandle(mThread);
	mThread = NULL;
	mState = State_None;
}